package com.lefevre.cms.web.action.payment;


import com.lefevre.cms.bean.*;
import com.lefevre.cms.bean.payment.PaymentLog;
import com.lefevre.cms.bean.payment.PaymentVerificationLog;
import com.lefevre.cms.bean.user.User;
import com.lefevre.cms.service.payment.PaymentService;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.service.user.UserService;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.utils.Verification;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 支付日志管理
 *
 * @author Lefevre
 */
@Controller
@RequestMapping("/control/paymentLog/manage")
public class PaymentLogManageAction {

    @Resource
    PaymentService paymentService;
    @Resource
    SettingService settingService;
    @Resource
    UserService userService;

    /**
     * 支付日志管理 详细显示
     *
     * @param model
     * @param paymentRunningNumber
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(params = "method=show", method = RequestMethod.GET)
    public String addUI(ModelMap model, String paymentRunningNumber, Long id,
                        HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, String> error = new HashMap<>();
        Map<String, Object> returnValue = new HashMap<>();
        if (paymentRunningNumber != null && !"".equals(paymentRunningNumber.trim())) {
            if (paymentRunningNumber.trim().length() > 9 && Verification.isPositiveInteger(paymentRunningNumber)) {
                PaymentLog paymentLog = paymentService.findPaymentLogByPaymentRunningNumber(paymentRunningNumber);
                if (paymentLog != null) {
                    returnValue.put("paymentLog", paymentLog);
                }
            }
            User _user = userService.findUserById(id);
            if (_user != null) {
                returnValue.put("currentUser", _user);
            }
        } else {
            error.put("paymentRunningNumber", "支付流水号不能为空");
        }
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
        }
    }

    /**
     * 支付校验日志分页
     *
     * @param paymentModule 支付模块 1.订单支付 3.售后服务换货/返修支付 5.用户充值
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "method=ajax_paymentVerificationLogPage", method = RequestMethod.GET)
    public String paymentVerificationLogPage(ModelMap model, PageForm pageForm,
                                             Integer paymentModule, Long parameterId, String userName) {

        Map<String, String> error = new HashMap<>();
        StringBuffer jpql = new StringBuffer("");

        List<Object> params = new ArrayList<>();
        //调用分页算法代码
        PageView<PaymentVerificationLog> pageView = new PageView<PaymentVerificationLog>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10);

        //当前页
        int firstindex = (pageForm.getPage() - 1) * pageView.getMaxresult();
        if (paymentModule != null && paymentModule > 0 && userName != null && !"".equals(userName.trim())) {

            jpql.append(" and o.parameterId=?").append((params.size() + 1));
            params.add(parameterId);

            jpql.append(" and o.userName=?").append((params.size() + 1));
            params.add(userName);

            jpql.append(" and o.paymentModule=?").append((params.size() + 1));//and o.code=?1
            params.add(paymentModule);


            //排序
            LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
            orderby.put("id", "desc");

            //删除第一个and
            String sql = StringUtils.difference(" and", jpql.toString());

            QueryResult<PaymentVerificationLog> qr = paymentService.getScrollData(PaymentVerificationLog.class, firstindex, pageView.getMaxresult(), sql, params.toArray(), orderby);


            //将查询结果集传给分页List
            pageView.setQueryResult(qr);
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, pageView));
        } else {
            error.put("paymentModule", "支付模块或用户名称不能为空");
        }
        return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
    }
}

